package com.swak.monitor.dubbo.web;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import org.apache.dubbo.common.URL;
import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Get;
import com.swak.annotation.RestApi;
import com.swak.entity.Result;
import com.swak.monitor.dubbo.service.RegistryServerSync;
import com.swak.utils.Maps;

/**
 * 
 * @author lifeng
 */
@RestApi(value = "DubboFullApi", path = "/api/monitor/:env/full")
public class FullApi {

	@Autowired
	private RegistryServerSync sync;

	/**
	 * 存储的所有的信息
	 * 
	 * @return
	 */
	@Get("/registrys")
	public Result registrys() {
		ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> cache = sync.getRegistryCache();
		Set<String> categorys = cache.keySet();
		Map<String, Map<String, Map<String, String>>> jsons = Maps.newHashMap();
		for (String category : categorys) {
			ConcurrentMap<String, Map<String, URL>> categoryValue = cache.get(category);
			Map<String, Map<String, String>> categoryStore = jsons.computeIfAbsent(category, (k) -> Maps.newHashMap());
			Set<String> services = categoryValue.keySet();
			for (String service : services) {
				Map<String, URL> serviceValue = categoryValue.get(service);
				Map<String, String> serviceStore = categoryStore.computeIfAbsent(service, (k) -> Maps.newHashMap());
				Set<String> sers = serviceValue.keySet();
				for (String ser : sers) {
					URL serValue = serviceValue.get(ser);
					serviceStore.put(ser, serValue.toFullString());
				}
			}
		}
		return Result.success(jsons);
	}
}